package com.hargasembako.core.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hargasembako.core.PMF;
import com.hargasembako.core.entity.ProductPrice;
import com.hargasembako.core.entity.Store;
import com.hargasembako.core.util.SearchJanitor;

@SuppressWarnings("serial")
public class StoreServlet extends HttpServlet {
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			doGet(req, resp);
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException {
		
		String action = req.getParameter("action");
		if(action.equals("storeLookup")) {
			storeLookup(req, resp);
		} else if (action.equals("storeCreate")){
			storeCreate(req, resp);
		} else if (action.equals("storeSave")){
			storeSave(req, resp);
		} else if (action.equals("storeDisplay")){
			storeDisplay(req, resp);
		} else if (action.equals("storeList")){
			storeList(req, resp);
		} else if (action.equals("storeEdit")){
			storeEdit(req, resp);
		} else if (action.equals("storeUpdate")){
			storeUpdate(req, resp);
		}
	}
	
	private void storeUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// fetch the account
		Key k = KeyFactory.createKey(Store.class.getSimpleName(), 
				new Integer(req.getParameter("storeId")).intValue());
		Store s = pm.getObjectById(Store.class, k);
		
		String name = req.getParameter("name");
		String address = req.getParameter("address");
		String city = req.getParameter("city");
		String state = req.getParameter("state");
		String phone = req.getParameter("phone");
		
		// update the store
		s.setName(name);
		s.setAddress(address);
		s.setCity(city);
		s.setState(state);
		s.setPhone(phone);
		
		// persist the entity
		try {
			pm.makePersistent(s);
		} finally {
			pm.close();
		}
		resp.sendRedirect("/store?action=storeList");
	}

	private void storeEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// fetch the account
		Key k = KeyFactory.createKey(Store.class.getSimpleName(), 
				new Integer(req.getParameter("storeId")).intValue());
		Store s = pm.getObjectById(Store.class, k);
		
		// pass the list to the jsp
		req.setAttribute("store", s);
		
		// forward the request to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/storeEdit.jsp");
		dispatcher.forward(req, resp);

	}	
	
	private void storeList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// query for the price
		String query = "select from " + Store.class.getName();
		List<Store> stores = (List<Store>) pm.newQuery(query).execute();
		
		// pass the list to the jsp
		req.setAttribute("stores", stores);
		
		// forward the request to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/storeList.jsp");
		dispatcher.forward(req, resp);

	}
	

	private void storeDisplay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// fetch the account
		Key k = KeyFactory.createKey(Store.class.getSimpleName(), 
				new Integer(req.getParameter("storeId")).intValue());
		Store s = pm.getObjectById(Store.class, k);
		// query for the price
		String query = "select from " + ProductPrice.class.getName() + " where storeId == " + req.getParameter("storeId");
		List<ProductPrice> prices = (List<ProductPrice>) pm.newQuery(query).execute();
		
		// pass the list to the jsp
		req.setAttribute("store", s);
		
		// pass the list to the jsp
		req.setAttribute("prices", prices);
		
		// forward the request to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/storeDisplay.jsp");
		dispatcher.forward(req, resp);

	}

	private void storeSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		// create the new account
		//String name, String address, String cityName, String stateName, String phone
		Store a = new Store(
			req.getParameter("name"),
			req.getParameter("address"),
			req.getParameter("city"),
			req.getParameter("state"),
			req.getParameter("phone")
		);
		
		// persist the entity
		try {
			pm.makePersistent(a);			
		} finally {
			pm.close();
		}
		resp.sendRedirect("/store?action=storeDisplay&storeId="+a.getId());
	}

	private void storeCreate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.sendRedirect("/storeCreate.jsp");
	}

	private void storeLookup(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		// create the persistence manager instance
		PersistenceManager pm = PMF.get().getPersistenceManager();

		// query for the entities by name
		String className = Store.class.getName();
		String queryString = req.getParameter("store");
		
		//String query = "select from " + Store.class.getName() + " where name like '"+req.getParameter("storeName")+"'";
		//List<Store> stores = (List<Store>) pm.newQuery(query).execute();
		
		List<Store> stores = SearchJanitor.search(className, queryString, pm);
		
		// pass the list to the jsp
		req.setAttribute("stores", stores);
		
		// forward the req to the jsp
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/storeLookup.jsp");
		dispatcher.forward(req, resp);	
	}

}
